package com.koushikdutta.ion.loader;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.os.Build.VERSION;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.future.SimpleFuture;
import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import com.koushikdutta.ion.loader.MediaFile.MediaFileType;
import java.io.File;
import java.net.URI;

public class VideoLoader extends SimpleLoader {
    @TargetApi(10)
    public static Bitmap createVideoThumbnail(String filePath) throws Exception {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(filePath);
        return retriever.getFrameAtTime();
    }

    static boolean mustUseThumbnailUtils() {
        return Build.MANUFACTURER.toLowerCase().contains("samsung");
    }

    public Future<BitmapInfo> loadBitmap(Context context, Ion ion, String key, String uri, int resizeWidth, int resizeHeight, boolean animateGif) {
        if (!uri.startsWith("file")) {
            return null;
        }
        final MediaFileType type = MediaFile.getFileType(uri);
        if (type == null || !MediaFile.isVideoFileType(type.fileType)) {
            return null;
        }
        final Future<BitmapInfo> ret = new SimpleFuture();
        final String str = uri;
        final String str2 = key;
        Ion.getBitmapLoadExecutorService().execute(new Runnable() {
            public void run() {
                File file = new File(URI.create(str));
                if (!ret.isCancelled()) {
                    try {
                        Bitmap bmp;
                        if (VideoLoader.mustUseThumbnailUtils() || VERSION.SDK_INT < 10) {
                            bmp = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), 1);
                        } else {
                            bmp = VideoLoader.createVideoThumbnail(file.getAbsolutePath());
                        }
                        if (bmp == null) {
                            throw new Exception("video bitmap failed to load");
                        }
                        Object info = new BitmapInfo(str2, type.mimeType, new Bitmap[]{bmp}, new Point(bmp.getWidth(), bmp.getHeight()));
                        info.loadedFrom = 1;
                        ret.setComplete(info);
                    } catch (Exception e) {
                        ret.setComplete(e);
                    }
                }
            }
        });
        return ret;
    }
}
